<?php
ob_implicit_flush();
include "../Lib/Include.php";
$Domain=$_GET["S"];
$Type=$_GET["T"];
switch ($Type){
    case 1:
        $GoogleCount=GetGoogle($Domain,0);
        echo $GoogleCount;
        break;
    case 2:
        $BaiduCount=GetBaidu($Domain,0);
        echo $BaiduCount;
        break;
    case 3:
        $BingCount=GetBing($Domain,0);
        echo $BingCount;
        break;
    case 4:
        $YahooCount=GetYahoo($Domain,0);
        echo $YahooCount;
        break;
    case 5:
        $SosoCount=GetSoso($Domain,0);
        echo $SosoCount;
        break;
    case 6:
        $YoudaoCount=GetYoudao($Domain,0);
        echo $YoudaoCount;
        break;
    case 7:
        $SogouCount=GetSogou($Domain,0);
        echo $SogouCount;
        break;
    case 11:
        $GoogleLCount=GetGoogle($Domain,1);
        echo $GoogleLCount;
        break;
    case 12:
        $BaiduLCount=GetBaidu($Domain,1);
        echo $BaiduLCount;
        break;
    case 13:
        $BingLCount=GetBing($Domain,1);
        echo $BingLCount;
        break;
    case 14:
        $YahooLCount=GetYahoo($Domain,1);
        echo $YahooLCount;
        break;
    case 15:
        $SosoLCount=GetSoso($Domain,1);
        echo $SosoLCount;
        break;
    case 16:
        $YoudaoLCount=GetYoudao($Domain,1);
        echo $YoudaoLCount;
        break;
    case 17:
        $SogouLCount=GetSogou($Domain,1);
        echo $SogouLCount;
        break;
    case 21:
        $SogouPCount=GetSogou($Domain,3);
        echo $SogouPCount;
        break;
    default:
        echo "信息查询出错";
}

//获取百度收录
function GetBaidu($url,$type){
	$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /s?wd=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /s?wd=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.baidu.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("找到相关结果约",$data);
		$Arr=spliti("个",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取谷歌收录
function GetGoogle($url,$type){
	$fp = fsockopen("www.google.cn", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.google.cn\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("获得约 <b>",$data);
		$Arr=spliti("</b> 条结果",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取微软BING收录
function GetBing($url,$type){
	$fp = fsockopen("www.bing.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url."&mkt=zh-CN HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.bing.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp);
		$Arr=spliti("条结果，共 ",$data);
		$Arr=spliti(" 条",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取雅虎收录
function GetYahoo($url,$type){
	$fp = fsockopen("www.yahoo.cn", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /s?p=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /s?p=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.yahoo.cn\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$Arr=spliti("找到相关网页约",$data);
		$Arr=spliti("条",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取搜搜收录
function GetSoso($url,$type){
	$fp = fsockopen("www.soso.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /q?w=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /q?w=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.soso.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		$Arr=spliti("搜索到约",$data);
		$Arr=spliti("项结果",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取有道收录
function GetYoudao($url,$type){
	$fp = fsockopen("www.youdao.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /search?q=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /search?q=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.youdao.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$Arr=spliti("<span class=\"fr\">共",$data);
		$Arr=spliti("条结果",$Arr[1]);
		if($Arr[0]!="")
			return $Arr[0];
		else
			return "-";
	}
}
//获取搜狗收录
function GetSogou($url,$type){
	$fp = fsockopen("www.sogou.com", 80, $errno, $errstr, 30);
	if(!$fp){
		echo "$errstr ($errno)<br />\n";
	}else{
		if($type==0){
			$out = "GET /web?query=site%3A".$url." HTTP/1.1\r\n" ;
		}else{
			$out = "GET /web?query=link%3A".$url." HTTP/1.1\r\n" ;
		}
		$out .= "Host: www.sogou.com\r\n" ;
		$out .= "Connection: Close\r\n\r\n" ; 
		fwrite($fp, $out);
		$data="";
		while (!feof($fp)) {
			$data.= fgets($fp, 128);
		}
		fclose($fp); 
		$data=mb_convert_encoding($data,"UTF-8","gb2312");
		if($type==3){
			$Arr=spliti("搜狗评级:",$data);
			$Arr=spliti("&nbsp;",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "-";
		}else{
			$Arr=spliti("<!--resultbarnum:",$data);
			$Arr=spliti("-->",$Arr[1]);
			if($Arr[0]!="")
				return $Arr[0];
			else
				return "-";
		}
	}
}
?>